// mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_member/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    age: '',
    radio: '1',
    memberImg: "",
    imgSrc: "",
    nickname: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarColor({
      backgroundColor: '#FFAA01',
      frontColor: '#ffffff',
    })
  },
  onChange(e) {
    console.log(e)
    this.setData({
      radio: e.detail
    })
  },
  chooseImg(e) {
    console.log(e)
    let imgsrc = e.detail.avatarUrl;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.uploadFile({
      url: urlStr,
      filePath: imgsrc,
      name: 'file',
      formData: null,
      success: (resdata) => {
        var res = JSON.parse(resdata.data);
        this.setData({
          imgSrc: res.data.img_url,
          img: res.data.img
        })
      },
      fail(e) {
        app.tips(e.msg)
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.ai-tongue-diagnosis.frontend.controllers.index.member-info")
    })
    if (data.result) {
      this.setData({
        age: data.data.info.age,
        radio: String(data.data.info.gender),
        nickname: data.data.info.member.nickname,
        imgSrc: data.data.info.member.avatar_image
      })
    } else {
      app.tips(data.msg)
    }
  },
  async memberSave() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.ai-tongue-diagnosis.frontend.controllers.index.store-member-info"),
      data: {
        gender: this.data.radio,
        age: this.data.age,
        nickname: this.data.nickname
      }
    })
    if (data.result) {
      app.tips(data.msg)
      wx.navigateBack({
        delta: 1
      })
    } else {
      app.tips(data.msg);
    }
  }
})